/* Reiteration of Boggle game
 * Authors: Jesse Mace, Brad Cavanaugh
 * Create Date: 7/25/2013
 * 
 */

package a08TeamProject;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextPane;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JToggleButton;


import java.awt.FlowLayout;

public class BoggleGUI extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JPanel highScorePanel;
	private JLabel lblScore;
	private JPanel scoringPanel;
	private JTextPane scoringText;
	private JPanel gameBoardPanel;
	private JLabel scoringLabel;
	private JPanel WordPanel;
	private JButton enterWordButton;
	private JToggleButton[][] gameBoard;
	private eventHandler theHandler = new eventHandler();
	private StringBuilder sb = new StringBuilder();
	private int sbCount = 0;
	private JTextPane FoundWordListTextPanel;
	private List<String> listOfFoundWords = new ArrayList<>();
	private JLabel usersScoreLabel;
	
	/**
	 * Create the frame.
	 */
	public BoggleGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(0, 0, 1000, 1000);
		setTitle("Boggle");
		
		createContentPane();
	}
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BoggleGUI frame = new BoggleGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		
	}

	/**
	 * Creates the basic Content Pane that all the other containers are organized in.
	 */
	private void createContentPane() {
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		createFoundWordPanel();
		
		createHighScorePanel();
		
		createScoringPanel();
		
		createGameBoard(theHandler);
		
	}

	/**
	 * Creates the Panel on the west side that displays the Found Word List.
	 */
	private void createFoundWordPanel() {
		WordPanel = new JPanel();
		contentPane.add(WordPanel, BorderLayout.WEST);
		WordPanel.setLayout(new GridLayout(2, 0, 0, 100));
		
		enterWordButton = new JButton("Enter Word");
		enterWordButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				final String word = sb.toString();
	
				for (int r = 0; r < 4; r++) {
					for (int c = 0; c < 4; c++) {
						if(gameBoard [r][c].isSelected())
							gameBoard [r][c].setSelected(false);
	
					}
				}
	
				listOfFoundWords.add("\n" + word);
				FoundWordListTextPanel.setText("Found Word List:" + listOfFoundWords.toString());
				
				//reset the stringbuilder and its counter for next input
				sb.delete(0, sb.capacity());
				sbCount = 0;
			}
		});
		
		FoundWordListTextPanel = new JTextPane();
		WordPanel.add(FoundWordListTextPanel);
		
		WordPanel.add(enterWordButton);
		
		
		
	}

	/**
	 * Creates the boggle game board in the center of the content pane illustrated by a 4x4 button matrix of randomly selected letters.
	 * @param listener
	 */
	private void createGameBoard(ActionListener listener) {
		gameBoardPanel = new JPanel();
		contentPane.add(gameBoardPanel, BorderLayout.CENTER);
		gameBoardPanel.setLayout(new GridLayout(4, 4, 1, 1));
		
		gameBoard = new JToggleButton[4][4];
		
		for (int r = 0; r < 4; r++) {
			for (int c = 0; c < 4; c++) {
				gameBoard [r][c] = new JToggleButton(randomLetterGenerator());
				gameBoard[r][c].setForeground(Color.YELLOW);
				gameBoard[r][c].setBackground(Color.BLUE);
				gameBoard[r][c].setFont(new Font("Georgia", 0, 50));
				gameBoard[r][c].addActionListener(listener);
				gameBoardPanel.add(gameBoard[r][c]);
				
			}
		}
	}

	/**
	 * creates a panel declaring how the scoring is calculated to the user and adds it to the content pane.
	 */
	private void createScoringPanel() {
		scoringPanel = new JPanel();
		scoringPanel.setLayout(new BorderLayout(0, 0));
		contentPane.add(scoringPanel, BorderLayout.NORTH);
		
		
		scoringLabel = new JLabel("Scoring:");
		scoringLabel.setFont(new Font("Times New Roman", 0, 24));
		scoringPanel.add(scoringLabel, BorderLayout.WEST);
		
		
		scoringText = new JTextPane();
		scoringText.setOpaque(false);
		scoringPanel.add(scoringText, BorderLayout.CENTER);
		scoringText.setForeground(new Color(0, 0, 0));
		scoringText.setFont(new Font("Times New Roman", 0, 24));
		scoringText.setEditable(false);
		scoringText.setText("3  4  5  6  7  8+  # of letters\r\n1  1  2  3  5  11  points");

		usersScoreLabel = new JLabel("Your Score:");
		scoringPanel.add(usersScoreLabel, BorderLayout.EAST);
		
		
	}

	/**
	 * creates a high score panel for the user and adds it to the content pane.
	 */
	private void createHighScorePanel() {
		highScorePanel = new JPanel();
		highScorePanel.setAlignmentX(Component.RIGHT_ALIGNMENT);
		contentPane.add(highScorePanel, BorderLayout.EAST);
		highScorePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		lblScore = new JLabel("High Scores:");
		lblScore.setAlignmentY(Component.TOP_ALIGNMENT);
		highScorePanel.add(lblScore);
	
	}
	
	/**
	 * Method randomly selects a letter of the alphabet.
	 * @return
	 */
	private String randomLetterGenerator(){
		
		Random r = new Random();
		Alphabet[] letters = Alphabet.values();
		return letters[r.nextInt(26)].toString();
		
	}

/**
 * Action listener that stores and creates a string of the buttons pressed on the boggle game board.
 * @author Jesse Mace
 *
 */
private class eventHandler implements ActionListener{

	@Override
	public void actionPerformed(ActionEvent e) {
		
		
		JToggleButton button = (JToggleButton)e.getSource();
		
		if(button.isSelected()){
			sb.append(button.getText());
			sbCount++;
		}
		
		if(button.isSelected() == false){
			sbCount--;
			sb.deleteCharAt(sbCount);
			
		}
		
		
		
		
		

	}
	
}
}



